-- ============================================
-- ROUE DE LA FORTUNE - ComputerCraft / CC:Tweaked
-- Nécessite : Advanced Computer + Advanced Monitor (couleur)
-- ============================================

local MISE = 1
local points = 100

-- Segments de la roue (multiplicateurs), disposes en cercle
local SEGMENTS = { 0, 1, 0, 2, 0, 1, 3, 0, 1, 2, 5, 0 }

local function segColor(mult)
    if mult >= 5 then return colors.magenta
    elseif mult >= 3 then return colors.orange
    elseif mult >= 2 then return colors.yellow
    elseif mult >= 1 then return colors.lightGray
    else return colors.gray end
end

local mon = peripheral.find("monitor")
if not mon then
    error("Aucun monitor trouve ! Colle un Advanced Monitor a cote du computer.")
end

mon.setTextScale(0.5)
local prevTerm = term.redirect(mon)
local w, h = term.getSize()

local function centerText(y, text, textColor, bgColor)
    term.setBackgroundColor(bgColor or colors.black)
    term.setTextColor(textColor or colors.white)
    local x = math.floor((w - #text) / 2) + 1
    term.setCursorPos(x, y)
    term.write(text)
end

local function fillArea(x1, y1, x2, y2, bg)
    term.setBackgroundColor(bg)
    for y = y1, y2 do
        term.setCursorPos(x1, y)
        term.write(string.rep(" ", x2 - x1 + 1))
    end
end

-- === AFFICHAGE DE LA ROUE (bande horizontale de segments) ===
local SEG_W = 6
local wheelY = math.floor(h / 2)
local wheelLeft = math.max(1, math.floor((w - SEG_W * 7) / 2) + 1) -- 7 segments visibles

local function drawWheel(highlightIndex)
    local visibleCount = math.floor(w / SEG_W)
    local half = math.floor(visibleCount / 2)
    fillArea(1, wheelY - 1, w, wheelY + 1, colors.black)

    for i = -half, half do
        local segIndex = ((highlightIndex - 1 + i) % #SEGMENTS) + 1
        local mult = SEGMENTS[segIndex]
        local x1 = wheelLeft + (i + half) * SEG_W
        local x2 = x1 + SEG_W - 1
        if x2 >= 1 and x1 <= w then
            local isCenter = (i == 0)
            term.setBackgroundColor(isCenter and colors.white or segColor(mult))
            term.setTextColor(isCenter and colors.black or colors.white)
            for x = math.max(1, x1), math.min(w, x2) do
                term.setCursorPos(x, wheelY)
                term.write(" ")
            end
            local label = "x" .. mult
            local lx = x1 + math.floor((SEG_W - #label) / 2)
            if lx >= 1 and lx <= w then
                term.setCursorPos(lx, wheelY)
                term.write(label)
            end
        end
    end

    -- pointeur au centre
    term.setBackgroundColor(colors.black)
    term.setTextColor(colors.red)
    term.setCursorPos(math.floor(w / 2) + 1, wheelY - 1)
    term.write("v")
end

local function drawHeader()
    fillArea(1, 1, w, wheelY - 2, colors.black)
    centerText(1, "=== CASINO COMPUTERCRAFT ===", colors.yellow)
    centerText(2, "ROUE DE LA FORTUNE", colors.white)
    centerText(3, "Points : " .. points, colors.lime)
end

-- === UNE MANCHE ===
local function spinWheel()
    local startIndex = math.random(1, #SEGMENTS)
    local steps = 30 + math.random(0, 10)
    local finalIndex

    for step = 1, steps do
        local idx = ((startIndex + step - 2) % #SEGMENTS) + 1
        drawHeader()
        drawWheel(idx)
        finalIndex = idx
        sleep(0.03 + (step / steps) * 0.15) -- ralentit progressivement
    end

    return finalIndex
end

local function playRound()
    if points < MISE then
        drawHeader()
        drawWheel(1)
        centerText(h - 1, "PLUS ASSEZ DE POINTS !", colors.red)
        sleep(2)
        return
    end
    points = points - MISE

    drawHeader()
    centerText(h - 1, "La roue tourne...", colors.yellow)

    local finalIndex = spinWheel()
    local mult = SEGMENTS[finalIndex]
    local gain = MISE * mult
    points = points + gain

    drawHeader()
    local msg, msgColor
    if gain > 0 then
        msg, msgColor = "Tombe sur x" .. mult .. " ! +" .. gain, colors.lime
    else
        msg, msgColor = "Tombe sur x0... perdu !", colors.red
    end
    centerText(h - 1, msg, msgColor)
    sleep(2.5)
end

-- === ECRAN D'ACCUEIL ===
local button = {}

local function drawIdle()
    term.setBackgroundColor(colors.black)
    term.clear()
    drawHeader()
    drawWheel(1)

    local label = "MISER (" .. MISE .. " point) ET TOURNER"
    local bw = #label + 4
    local x1 = math.max(1, math.floor((w - bw) / 2) + 1)
    local y = h - 1
    term.setBackgroundColor(colors.green)
    term.setTextColor(colors.white)
    for x = x1, math.min(w, x1 + bw - 1) do
        term.setCursorPos(x, y)
        term.write(" ")
    end
    term.setCursorPos(x1 + 2, y)
    term.write(label)
    button = { x1 = x1, x2 = x1 + bw - 1, y = y }
end

-- === PROGRAMME PRINCIPAL ===
drawIdle()

while true do
    local event, side, x, y = os.pullEvent("monitor_touch")
    if x >= button.x1 and x <= button.x2 and y == button.y then
        playRound()
        drawIdle()
    end
end